package com.geekaca.news.controller.admin;

import com.geekaca.news.domain.Config;
import com.geekaca.news.service.ConfigService;
import com.geekaca.news.utils.NewsConstants;
import com.geekaca.news.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class ConfigController {
    @Autowired
    private ConfigService configService;

    @GetMapping("/configurations")
    public String config(HttpServletRequest request) {
        request.setAttribute("path", "configurations");
        HashMap<String, String> configurations = new HashMap<>();
        List<Config> configs = configService.getAllConfigs();
        configs.forEach(config -> {
            configurations.put(config.getConfigName(), config.getConfigValue());
        });
        request.setAttribute("configurations", configurations);
        return "admin/configuration";
    }

    @PostMapping("/configurations/website")
    @ResponseBody
    public Result webSiteConfig(@RequestParam("websiteName") String websiteName,
                                @RequestParam("websiteDescription") String websiteDescription,
                                @RequestParam("websiteLogo") String websiteLogo,
                                @RequestParam("websiteIcon") String websiteIcon
    ) {
        Result result = new Result();
        List<Config> configs = new ArrayList<>();

        configs.add(new Config("websiteName", websiteName));
        configs.add(new Config("websiteDescription", websiteDescription));
        configs.add(new Config("websiteLogo", websiteLogo));
        configs.add(new Config("websiteIcon", websiteIcon));
        for (Config config : configs) {
            int i = configService.updateWebSiteConfig(config);
            if(i>0){
                result.setResultCode(NewsConstants.RESULT_OK);
                result.setMessage("站点信息修改成功");
                result.setData(config);
            }else{
                result.setResultCode(NewsConstants.RESULT_FAIL);
                result.setMessage("站点信息修改失败");
                result.setData(config);
                break;
            }
        }
        return result;
    }

    @PostMapping("/configurations/userInfo")
    @ResponseBody
    public Result userInfoConfig(@RequestParam("yourAvatar") String yourAvatar,
                                @RequestParam("yourName") String yourName,
                                @RequestParam("yourEmail") String yourEmail

    ) {
        Result result = new Result();
        List<Config> configs = new ArrayList<>();
        configs.add(new Config("yourAvatar", yourAvatar));
        configs.add(new Config("yourName", yourName));
        configs.add(new Config("yourEmail", yourEmail));
        for (Config config : configs) {
            int i = configService.updateWebSiteConfig(config);
            if(i>0){
                result.setResultCode(NewsConstants.RESULT_OK);
                result.setMessage("个人信息修改成功");
                result.setData(config);
            }else{
                result.setResultCode(NewsConstants.RESULT_FAIL);
                result.setMessage("个人信息修改失败");
                result.setData(config);
                break;
            }
        }
        return result;
    }

    @PostMapping("/configurations/footer")
    @ResponseBody
    public Result footerConfig(@RequestParam("footerAbout") String footerAbout,
                                 @RequestParam("footerICP") String footerICP,
                                 @RequestParam("footerCopyRight") String footerCopyRight,
                                 @RequestParam("footerPoweredBy") String footerPoweredBy,
                                 @RequestParam("footerPoweredByURL") String footerPoweredByURL

    ) {
        Result result = new Result();
        List<Config> configs = new ArrayList<>();
        configs.add(new Config("footerAbout", footerAbout));
        configs.add(new Config("footerICP", footerICP));
        configs.add(new Config("footerCopyRight", footerCopyRight));
        configs.add(new Config("footerPoweredBy", footerPoweredBy));
        configs.add(new Config("footerPoweredByURL", footerPoweredByURL));
        for (Config config : configs) {
            int i = configService.updateWebSiteConfig(config);
            if(i>0){
                result.setResultCode(NewsConstants.RESULT_OK);
                result.setMessage("底部设置修改成功");
                result.setData(config);
            }else{
                result.setResultCode(NewsConstants.RESULT_FAIL);
                result.setMessage("底部设置修改失败");
                result.setData(config);
                break;
            }
        }
        return result;
    }
}
